
# libraries

library(readxl)
library(tidyverse)
library(hrbrthemes)
library(reshape2)
library(ggpubr)

# files

meta_SPÖ <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/SPÖ_time.xlsx")
meta_ÖVP <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/ÖVP_time.xlsx")

ÖVP_cluster <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/ÖVP_cluster_final.xlsx")
SPÖ_cluster <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/SPÖ_cluster_final.xlsx")

                      ################
                      #####ÖVP########
                      ################

# select information for time series analysis

meta_ÖVP <- meta_ÖVP %>%
  select(LastName,FirstName,GP_dt)

# merge

ÖVP <- merge(meta_ÖVP,ÖVP_cluster,by=c("LastName","FirstName"))
ÖVP <- ÖVP %>%
  select(GP_dt,clust)

# percentage of MPs with political positions

ÖVP$"Rural Fraction"[ÖVP$"clust" == "Rural Fraction"]    <- 1
ÖVP$"Party Animals"[ÖVP$"clust" == "Party Animals"]    <- 1
ÖVP$"Party Locals"[ÖVP$"clust" == "Party Locals"]    <- 1
ÖVP$"Economic Fraction"[ÖVP$"clust" == "Economic Fraction"]    <- 1

ÖVP$clust <- NULL
ÖVP[is.na(ÖVP)]<-0
ÖVP$counter <- 1

ÖVP <- ÖVP %>%
  group_by(GP_dt)  %>%
  summarise_all(sum)

ÖVP$"Rural Fraction" <- round((ÖVP$"Rural Fraction"/ÖVP$counter)*100,digits=1)
ÖVP$"Party Animals" <- round((ÖVP$"Party Animals"/ÖVP$counter)*100,digits=1)
ÖVP$"Party Locals" <- round((ÖVP$"Party Locals"/ÖVP$counter)*100,digits=1)
ÖVP$"Economic Fraction" <- round((ÖVP$"Economic Fraction"/ÖVP$counter)*100,digits=1)

ÖVP$counter <- NULL
ÖVP <-   melt(ÖVP, id.vars=c("GP_dt"))

# prepare for visualization

ÖVP$GP_dt <- as.factor(ÖVP$GP_dt)
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="5"] <- "5 (1945-49)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="6"] <- "6 (1949-53)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="7"] <- "7 (1953-56)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="8"] <- "8 (1956-59)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="9"] <- "9 (1959-62)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="10"] <- "10 (1962-66)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="11"] <- "11 (1966-70)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="12"] <- "12 (1970-71)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="13"] <- "13 (1971-75)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="14"] <- "14 (1975-79)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="15"] <- "15 (1979-83)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="16"] <- "16 (1983-86)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="17"] <- "17 (1986-90)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="18"] <- "18 (1990-94)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="19"] <- "19 (1994-96)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="20"] <- "20 (1996-99)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="21"] <- "21 (1999-02)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="22"] <- "22 (2002-06)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="23"] <- "23 (2006-08)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="24"] <- "24 (2008-13)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="25"] <- "25 (2013-17)"
levels(ÖVP$GP_dt)[levels(ÖVP$GP_dt)=="26"] <- "26 (2017-19)"

# visualize

ÖVP_plot<- ggplot(ÖVP, aes(x=GP_dt,y=variable, fill= value)) +
  geom_tile() +
  scale_fill_distiller(palette = "Spectral", limits = c(min(ÖVP$value), max(ÖVP$value)))+
  theme_ipsum_rc(base_family = "Hind")+
  theme(axis.text.x  = element_text(angle=90,hjust=1,size=11),
        axis.text.y  = element_text(vjust=0.0,size=11))+
  #theme(legend.title=element_text("% of all MPs "))+
  guides(fill=guide_legend(title="% of all ÖVP MPs"))+
  labs(y = "", x= "legislation", title="ÖVP")+
  theme(plot.title = element_text(face="bold"))+
  geom_text(aes(label = round(value, 1)), color="black", size=2.6) 

ÖVP_plot

                      ################
                      #####SPÖ########
                      ################

# select information for time series analysis

meta_SPÖ <- meta_SPÖ %>%
  select(LastName,FirstName,GP_dt)

#merge

SPÖ <- merge(meta_SPÖ,SPÖ_cluster,by=c("LastName","FirstName"))
SPÖ <- SPÖ %>%
  select(GP_dt,clust)

# percentage of MPs with political positions

SPÖ$"Majors"[SPÖ$"clust" == "Majors"]    <- 1
SPÖ$"Party Animals"[SPÖ$"clust" == "Party Animals"]    <- 1
SPÖ$"Party Locals"[SPÖ$"clust" == "Party Locals"]    <- 1
SPÖ$"Organised Labour Repr."[SPÖ$"clust" == "Organised Labour Repr."]    <- 1

SPÖ$clust <- NULL
SPÖ[is.na(SPÖ)]<-0
SPÖ$counter <- 1

SPÖ <- SPÖ %>%
  group_by(GP_dt)  %>%
  summarise_all(sum)

SPÖ$"Majors" <- round((SPÖ$"Majors"/SPÖ$counter)*100,digits=1)
SPÖ$"Party Animals" <- round((SPÖ$"Party Animals"/SPÖ$counter)*100,digits=1)
SPÖ$"Party Locals" <- round((SPÖ$"Party Locals"/SPÖ$counter)*100,digits=1)
SPÖ$"Organised Labour Repr." <- round((SPÖ$"Organised Labour Repr."/SPÖ$counter)*100,digits=1)

SPÖ$counter <- NULL
SPÖ <-   melt(SPÖ, id.vars=c("GP_dt"))

# prepare for visualization

SPÖ$GP_dt <- as.factor(SPÖ$GP_dt)
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="5"] <- "5 (1945-49)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="6"] <- "6 (1949-53)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="7"] <- "7 (1953-56)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="8"] <- "8 (1956-59)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="9"] <- "9 (1959-62)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="10"] <- "10 (1962-66)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="11"] <- "11 (1966-70)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="12"] <- "12 (1970-71)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="13"] <- "13 (1971-75)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="14"] <- "14 (1975-79)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="15"] <- "15 (1979-83)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="16"] <- "16 (1983-86)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="17"] <- "17 (1986-90)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="18"] <- "18 (1990-94)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="19"] <- "19 (1994-96)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="20"] <- "20 (1996-99)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="21"] <- "21 (1999-02)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="22"] <- "22 (2002-06)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="23"] <- "23 (2006-08)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="24"] <- "24 (2008-13)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="25"] <- "25 (2013-17)"
levels(SPÖ$GP_dt)[levels(SPÖ$GP_dt)=="26"] <- "26 (2017-19)"

# visualize

SPÖ_plot<- ggplot(SPÖ, aes(x=GP_dt,y=variable, fill= value)) +
  geom_tile() +
  scale_fill_distiller(palette = "Spectral", limits = c(min(SPÖ$value), max(SPÖ$value)))+
  theme_ipsum_rc(base_family = "Hind")+
  theme(axis.text.x  = element_text(angle=90,hjust=1,size=11),
        axis.text.y  = element_text(vjust=0.0,size=11))+
  #theme(legend.title=element_text("% of all MPs "))+
  guides(fill=guide_legend(title="% of all SPÖ MPs"))+
  labs(y = "", x= "legislation", title="SPÖ")+
  theme(plot.title = element_text(face="bold"))+
  geom_text(aes(label = round(value, 1)), color="black", size=2.6) 

SPÖ_plot

                              ###############
                              ###export######
                              ###############

ggarrange(ÖVP_plot, SPÖ_plot,
          #labels = c("", ""),
          ncol = 1, nrow = 2) %>%
  ggexport(filename = "/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/figures/figure5.png", width = 1000,
           height = 600,
           pointsize = 30)


